import Vue from 'vue' //引入 Vue
import Vuex from 'vuex' //引入 Vuex
import commons from './modules/commons'
import users from './modules/users'

Vue.use(Vuex)
const store = new Vuex.Store({
    //定义全局对象或者属性，属于会话级别。
    state: {
        //使vuex不受页面影响生命周期状态：例如webSiteData
        //
        // 使用本地缓存，浏览器关闭，用户状态消失。
        //网站资料数据
        webSiteData: JSON.parse(sessionStorage.getItem('webSiteInfo.simoniu.com')),
        //版块列表数据
        catalogListData: JSON.parse(sessionStorage.getItem('catalogList.simoniu.com')),
        //是否已经登录状态数据
        currentLoginStatusData: JSON.parse(sessionStorage.getItem('currentLoginStatus.simoniu.com')),
        //是否是管理员状态数据
        currentIsAdminStatusData:JSON.parse(sessionStorage.getItem('currentIsAdminStatus.simoniu.com')),
        //当前的板块信息,注意就是纯字符串
        currentCatalogNameData: sessionStorage.getItem('currentCatalogName.simoniu.com'),
        //当前的菜单信息。记录当前用户选中了那个菜单。
        currentMenuData: sessionStorage.getItem('currentMenuData.simoniu.com'),
        //当前登录成功的用户信息
        currentLoginUserInfo: JSON.parse(localStorage.getItem("loginSuccessUserInfo.simoniu.com")),
        //当前NavBar激活index
        currentTabBarActiveIndex: 0
    },

    getters: {
        //判断网站资料是否已经初始化
        isInitWebSiteInfo(state) {
            //不为null说明初始化了。
            //console.log("in getters=>"+state.webSiteData);
            return state.webSiteData !== null;
        }

    },
    mutations: {
        setCurrentLoginStatusData(state, value) {
            state.currentLoginStatusData = value;
            sessionStorage.setItem('currentLoginStatus.simoniu.com', JSON.stringify(value));
        },

        setCurrentIsAdminStatusData(state, value) {
            state.currentIsAdminStatusData = value;
            sessionStorage.setItem('currentIsAdminStatus.simoniu.com', JSON.stringify(value));
        },

        setWebSiteData(state, value) {
            state.webSiteData = value;
            sessionStorage.setItem('webSiteInfo.simoniu.com', JSON.stringify(value));
        },

        setCatalogListData(state, value) {
            state.catalogListData = value;
            sessionStorage.setItem('catalogList.simoniu.com', JSON.stringify(value));
        },

        setCurrentCatalogNameData(state,value){
            //console.log('setCurrentCatalogNameData->value:'+value);
            state.currentCatalogNameData = value;
            sessionStorage.setItem('currentCatalogName.simoniu.com',value);
        },

        setCurrentMenuData(state,value){
            state.currentMenuData = value;
            sessionStorage.setItem('currentMenuData.simoniu.com',value);
        },

        setCurrentTabBarActiveIndex(state,value){
            state.currentTabBarActiveIndex = value;
        }
    },

    actions: {
        actionSetCurrentLoginStatusData(context, value){
            context.commit("setCurrentLoginStatusData", value);
        },

        actionSetCurrentIsAdminStatusData(context, value){
            context.commit("setCurrentIsAdminStatusData", value);
        },

        actionSetWebSiteData(context, value) {
            context.commit("setWebSiteData", value);
        },
        actionSetCatalogListData(context, value) {
            context.commit("setCatalogListData", value);
        },
        actionSetCurrentCatalogNameData(context,value){
            context.commit("setCurrentCatalogNameData",value);
        },
        actionSetCurrentMenuData(context,value){
            context.commit("setCurrentMenuData",value);
        },
        actionSetCurrentTabBarActiveIndex(context,value){
            context.commit("setCurrentTabBarActiveIndex",value);
        }
    },
    modules: {
        users,
        commons
    }
})

export default store